package com.epam.edu.task02.Strings.textParts;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.epam.edu.task02.Strings.ConfigurationRegex;

public class Sentence {
	private static Logger logger = Logger.getLogger(Sentence.class);
	private int numberOfWord;
	private List<SentencePart> sentenceParts = new LinkedList<SentencePart>();
	private ConfigurationRegex regex = new ConfigurationRegex();

	public Sentence(String sentence) {
		logger.info("Add a sentence");
		splitSentenceIntoParts(sentence);
		setNumberofword();
	}

	private int countNumberOfOccurrencesOfWordsInSentence(String word) {
		int count = 0;
		for (int i = 0; i < numberOfWord - 1; i++) {
			if (sentenceParts.get(i).toString().equals(word)) {
				count++;
			}
		}
		return count;
	}

	public int getNumberofword() {
		return numberOfWord;
	}

	public int getNumberSimilarWords() {
		int maxcount = 0;
		for (int i = 0; i < numberOfWord - 1; i++) {
			if (sentenceParts.get(i) instanceof Word) {
				int count = countNumberOfOccurrencesOfWordsInSentence(sentenceParts
						.get(i).toString());
				if (maxcount < count) {
					maxcount = count;
				}
			}
		}
		return maxcount;
	}

	public SentencePart getSentencePart(int index) {
		return sentenceParts.get(index);
	}

	private String makeSentence() {
		StringBuilder sentence = new StringBuilder();
		for (int i = 0; i < sentenceParts.size(); i++) {
			if (i == sentenceParts.size() - 2
					|| (sentenceParts.get(i) instanceof Word
							&& sentenceParts.get(i + 1) instanceof Punctuation && !sentenceParts
							.get(i + 1).toString().equals("-"))) {
				sentence.append(sentenceParts.get(i).toString());
			} else {
				sentence.append(sentenceParts.get(i).toString() + " ");
			}
		}
		return sentence.toString();
	}

	private void setNumberofword() {
		this.numberOfWord = sentenceParts.size();
	}

	public void setSentencePart(int index, SentencePart sentencePart) {
		this.sentenceParts.set(index, sentencePart);
	}

	private void splitSentenceIntoParts(String sentence) {
		logger.info("Divide the sentence into words");
		String regex = ConfigurationRegex.getPunctuation();
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(sentence);
		int wordbegin = 0;
		while (matcher.find()) {
			int indexbegin = matcher.start();
			int indexend = matcher.end();
			String partSentence, punctuation;
			partSentence = sentence.substring(wordbegin, indexbegin + 1);
			punctuation = sentence.substring(indexbegin + 1, indexend);
			SentencePart partWord, partPunctuation;
			String words[] = partSentence.split("\\s");
			for (String word : words) {
				if (!" ".equals(word)) {
					partWord = new Word(word);
					this.sentenceParts.add(partWord);
				}
			}
			partPunctuation = new Punctuation(punctuation);
			this.sentenceParts.add(partPunctuation);
			wordbegin = indexend + 1;
		}
	}

	public void swapFirstToLast() {
		if (sentenceParts.size() != 0) {
			SentencePart dd = this.sentenceParts.get(0);
			SentencePart buffer = new Word(this.sentenceParts.get(0).toString());
			this.sentenceParts.set(0,
					this.sentenceParts.get(this.sentenceParts.size() - 2));
			this.sentenceParts.set(this.sentenceParts.size() - 2, buffer);
		}
	}

	@Override
	public String toString() {
		return makeSentence();
	}

}
